{% extends "base.html" %}

{% block content %}
    

{% load comments %}


    <script type="text/javascript">
    function progress_changed(data) {
        $('#progress_bar').attr("style", "width: " + data.new_progress + "%;");
        if (data.done) {
            window.location.reload(true);
        }
    };
    
    $(document).ready( function() {
        $('.progress_step').click(function() {
            Dajaxice.achievements.progress_step(progress_changed, { 'fulfilment_id':$(this).data("fulfilment"), 'add':true });
        });
        
        $('.progress_step_minus').click(function() {
            Dajaxice.achievements.progress_step(progress_changed, { 'fulfilment_id':$(this).data("fulfilment"), 'add':false });
        });
        
        $("#file_select").change(function() {
            $("#file_path").val($(this).val());
        });
        
        $("#file_button").click(function() {
            $("#file_select").click();
        });
    });
    </script>

    {% if achievement %}
        {% if achievement.author == user %}
        <div id="editAchievement" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="evidenceModalLabel" aria-hidden="true">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="evidenceModalLabel">Upraviť achievement</h3>
            </div>
            
            <div class="modal-body">
                
            </div>
            
            <div class="modal-footer">
                <button class="btn btn-success pull-left" data-dismiss="modal" aria-hidden="true">Uložiť</button>
                <button class="btn" data-dismiss="modal" aria-hidden="true">Zavrieť</button>
            </div>
            
        </div>
        {% endif %}
    
        {% if achievement.evidenceRequired %}
        <div id="evidenceModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="evidenceModalLabel" aria-hidden="true">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="evidenceModalLabel">Zaslať dôkaz</h3>
            </div>
            
            <div class="modal-body">
                <form action="." method="post" enctype="multipart/form-data">{% csrf_token %}
                    <textarea placeholder="Popis dôkazu..." class="span12" rows="3" cols="80" name="evidence_desc"></textarea>
                    
                    <input  id="file_select" name="evidence_file" type="file" accept="application/x-compress,application/x-compressed,application/x-compressed,application/x-zip-compressed,application/zip,multipart/x-zip,image/*" style="display:none;" />
                    <div class="input-append">
                        <input id="file_path" class="input-xlarge" type="text" placeholder="Súbor (obrázok, zip archív...)">
                        <a class="btn" id="file_button">Prehľadávať</a>
                    </div>
                    <span>Obe položky sú povinné.</span>
                    
                    
                    <br /><br />
                    
                    <button type="submit" class="btn btn-block btn-success pull-left">Odoslať</button>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn" data-dismiss="modal" aria-hidden="true">Zavrieť</button>
            </div>
            
        </div>
        {% endif %}
    
    
        <div class="container-fluid">
            <div class="row-fluid">
                <div class="span3">
                
                {% if achievement.image %}
                    <img src="/{{ achievement.image.url }}" class="img-rounded" />
                {% else %}
                    <img src="/files/achievements/default.png" class="img-rounded" />
                {% endif %}
                
                <!--Sidebar content-->
                </div>
                
                <div class="span9">
                
                    <div class="row-fluid">
                        
                        <h1>
                        {{ achievement.name }} {{ result_list }}
                        
                        {% if achievement.author == user %}
                        <a href="#editAchievement" class="btn" role="button" data-toggle="modal"><i class="icon-white icon-pencil"></i> Upraviť</a>
                        {% endif %}

                        <div class="pull-right">
                        
                        {% if user.is_authenticated %}
                        
                            {% if fulfilment %}
                                
                                {% if fulfilment.progress == achievement.progressMax %}
                                    {% if achievement.evidenceRequired and not fulfilment.awarded %}
                                    
                                    {% if fulfilment.evidence %}
                                    
                                    <a href="#" id="evidenceRequiredBtn" class="btn btn-warning btn-large  disabled" rel="popover" data-placement="bottom" data-content="Pre splnenie tohto achievementu je nutné dodať dôkaz o jeho skutočnom splnení, ktorý ste už odoslali autorovi, prosím počkajte na potvrdenie." data-original-title="Achievement vyžaduje dôkaz">Dôkaz odoslaný</a>
                                    
                                    {% else %}
                                    <a href="#" id="evidenceRequiredBtn" class="btn btn-warning btn-large  disabled" rel="popover" data-placement="bottom" data-content="Pre splnenie tohto achievementu je nutné dodať dôkaz o jeho skutočnom splnení!" data-original-title="Achievement vyžaduje dôkaz">Vyžaduje dôkaz</a>
                                    
                                    <a href="#evidenceModal" class="btn btn-success btn-large add_evidence" data-fulfilment="{{fulfilment.id}}" role="button" data-toggle="modal"><i class="icon-white icon-file"></i></a>
                                    
                                    {% endif %}
                                    
                                    {% else %}
                                    
                                    <a href="#" id="completedBtn" class="btn btn-success btn-large disabled" rel="popover" data-placement="bottom" data-content="Tento achievement už bol splnený!" data-original-title="Achievement splnený">Splnený</a>
                                    
                                    {% endif %}
                                {% else %}
                                    <a href="#" class="btn btn-success btn-large progress_step_minus" data-fulfilment="{{fulfilment.id}}" ><i class="icon-white icon-minus"></i></a>
                                    
                                    <a href="#" id="completedBtn" class="btn btn-warning btn-large disabled" rel="popover" data-placement="bottom" data-content="Tento achievement už bol pridaný, no nebol ešte splnený!" data-original-title="Achievement v priebehu plnenia">V priebehu plnenia</a>
                                    
                                    <a href="#" class="btn btn-success btn-large progress_step" data-fulfilment="{{fulfilment.id}}" ><i class="icon-white icon-plus"></i></a>
                                {% endif %}
                                
                                <script>$(function () { $("#completedBtn").popover();});</script>
                            {% else %}
                            
                                {% if achievement.past_deadline %}
                                    <a href="#" id="pastdeadlineBtn" class="btn btn-danger btn-large pull-right disabled" rel="popover" data-placement="bottom" data-content="Dátum do ktorého bolo nutné tento achievment splniť uplynul!" data-original-title="Neskoro!">Čas uplynul</a>
                                        <script>$(function () { $("#pastdeadlineBtn").popover();});</script>
                                {% else %} 
                                    {% if not missing_dep %}
                                        {% if achievement.progressMax > 0 %}
                                            <a href="{% url join_achievement achievement_id=achievement.uuid %}" class="btn btn-info btn-large pull-right">Pridať sa!</a>
                                        {% else %}
                                            <a href="{% url join_achievement achievement_id=achievement.uuid %}" class="btn btn-info btn-large pull-right">Splniť!</a>
                                        {% endif %}                                
                                    {% else %}
                                        <a href="#" id="missingdepBtn" class="btn btn-block btn-danger btn-large pull-right disabled" rel="popover" data-placement="bottom" data-content="Nesplnili ste všetky požadované závislosti!" data-original-title="Chýbaju závislosti">Chýbajú závislosti</a>
                                        <script>$(function () { $("#missingdepBtn").popover();});</script>
                                    {% endif %}
                                    
                                {% endif %}
                                
                            {% endif %}
                            
                        {% else %}
                            <a href="#" id="button" class="btn btn-large pull-right disabled" rel="popover" data-placement="bottom" data-content="Musíte sa prihlásiť, aby ste sa mohli pridať k tomuto achievementu!" data-original-title="Nie ste prihlásení">Pridať sa!</a>
                            <script>$(function () { $("#button").popover();});</script>
                        {% endif %}
                        
                        </div>
                        </h1>
                        
                    </div>
                
                
                    <div class="row-fluid">
                        <table class='table table-condensed' style="margin-top:5px;">
                        <tbody>
                            
                            <tr>
                                <td><i class="icon-white icon-user"></i> Autor</td>
                                {% if achievement.author %}
                                    <td><a href="{% url show_user  user_id=achievement.author %}">{{achievement.author}}</a></td>
                                {% else %}
                                    <td><span class="label label-info">Spoločný</span></td>
                                {% endif %}
                                
                            </tr>
                            <tr>
                                <td><i class="icon-white icon-calendar"></i> Vytvorený</td>
                                <td>{{achievement.pub_date|date:"D, d E Y"}}</td>
                            </tr>
                            <tr>
                                <td><i class="icon-white icon-gift"></i> Odmena</td>
                                <td>
                                {% if achievement.points == 0 %}
                                    <span class="label">Bez odmeny</span>
                                {% else %}
                                    {% if achievement.points == 1 %}
                                        <span class="badge badge-info">{{ achievement.points }}</span> bod
                                    {% else %}
                                        <span class="badge badge-info">{{ achievement.points }}</span> bodov
                                    {% endif %}
                                {% endif %}
                                </td>
                            </tr>
                            <tr>
                                <td><i class="icon-white icon-tasks"></i> Priebeh plnenia</td>
                                <td>
                                
                                {% if achievement.progressMax > 0 %}
                                    {% if user.is_authenticated and fulfilment %}
                                    <div class="progress progress-success progress-striped" style="margin-top:20px;">
                                        <div class="bar" id="progress_bar" style="width: {% widthratio fulfilment.progress fulfilment.achievement.progressMax 100 %}%;"></div>
                                    </div>
                                    {% else %}
                                    {{ achievement.progressMax }} krokov
                                    {% endif %}
                                {% else %}
                                    <span class="label label-info">Žiaden</span>
                                {% endif %}
                                
                                </td>
                            </tr>
                            
                            {% if achievement.deadline %}
                            <tr>
                                <td><i class="icon-white icon-fire"></i> Deadline</td>
                                
                                <td>
                                {% if achievement.past_deadline %}
                                    Do <span class="label label-important">{{achievement.deadline|date:"D, d E Y"}}</span>
                                    o <span class="label label-important">{{achievement.deadline|date:"H:i:s T"}}</span>
                                {% else %}
                                    Do <span class="label label-success">{{achievement.deadline|date:"D, d E Y"}}</span>
                                    o <span class="label label-success">{{achievement.deadline|date:"H:i:s T"}}</span>
                                {% endif %}
                                
                                </td>
                            </tr>
                            {% endif %}
                            
                            {% if achievement.parse_attributes|safe %}
                            <tr>
                                <td><i class="icon-white icon-th-large"></i> Atribúty</td>
                                
                                <td>
                                {{ achievement.parse_attributes|safe }}
                                </td>
                            </tr>
                            {% endif %}
                        <tbody>
                    </table>
                    </div>
                
                
                </div>
            </div>
            
            <br>
            
            <div class="row-fluid">
                <div class="span12 well well-small">
                    <legend><i class="icon-white icon-align-left" style="margin-top:5px;"></i> Popis</legend>
                    <p>{{ achievement.description|safe }}</p>
                </div>
            </div>
            
            {% if dependencies %}
                <br/>
                <div class="row-fluid">
                    <div class="span12">
                    
                        <div class="well well-small">
                    
                        <legend><i class="icon-white icon-asterisk" style="margin-top:5px;"></i> Závisí na</legend>
                        <table class='table table-striped table-hover table-bordered'>
                            <thead>        
                                <tr>
                                    <th>Achievement</th>
                                    <th>Odmena</th>
                                    <th>Pridaný</th>
                                    {% if dep.has_attributes %}<th>Atribúty</th>{% endif %}
                                    <th>Splnený</th>
                                </tr>
                            </thead>
                            <tbody>
                            {% for dep, cmp in dependencies.iteritems %}
                                <tr>
                                    <td><a href="{% url show_one_achievement achievement_id=dep.uuid %}">{{dep.name}}</a></td>
                                    <td>
                                    {% if dep.points == 0 %}
                                        <span class="label">Bez odmeny</span>
                                    {% else %}
                                        {% if dep.points == 1 %}
                                            <span class="badge badge-info">{{ dep.points }}</span> bod
                                        {% else %}
                                            <span class="badge badge-info">{{ dep.points }}</span> bodov
                                        {% endif %}
                                    {% endif %}
                                    </td>
                                    <td>{{ dep.pub_date }}</td>
                                    
                                    {% if dep.has_attributes %}
                                    <td>
                                    {{ dep.parse_attributes|safe }}
                                    </td>
                                    {% endif %}
                                    
                                    <td>
                                    {% if cmp %}
                                        <span class="label label-success" style="margin-right:5px;">Áno</span>
                                    {% else %}
                                        <span class="label label-important" style="margin-right:5px;">Nie</span>
                                    {% endif %}
                                    </td>
                                </tr>
                            {% endfor %}
                            <tbody>
                        </table>
                        
                        </div>
                        
                    </div>
                </div>
            {% endif %}
            
            {% if subachievements %}
                <br/>
                <div class="row-fluid">
                    <div class="span12">
                    
                        <div class="well well-small">
                    
                        <legend><i class="icon-white icon-list" style="margin-top:5px;"></i> Podúlohy</legend>
                        <table class='table table-striped table-bordered'>
                            <thead>
                                <tr>
                                    <th>Názov</th>
                                    <th>Popis</th>
                                    <th colspan="2">Odmena</th>
                                </tr>
                            </thead>
                            <tbody>
                            {% for sub, cmp in subachievements.iteritems %}
                                <tr>
                                
                                    <td>{{sub.name}}</td>
                                    <td>{{sub.description}}</td>
                                    <td><div class="badge pull-left">{{sub.points}}</div>
                                    </td>
                                    <td>
                                    
                                    {% if user.is_authenticated and not missing_dep and fulfilment %}
                                        {% if not cmp %}
                                            <a href="{% url join_subachievement achievement_id=achievement.uuid sub_id=sub.id %}" class="btn btn-info pull-left">Splniť</a>
                                        {% else %}
                                            <a class="btn btn-success pull-left disabled">Splnená</a>
                                        {% endif %}
                                    {% else %}
                                        <a href="#" id="complete{{sub.id}}Btn" class="btn pull-left disabled" rel="popover" data-placement="top" data-content="Musíte byt prihlásení a pridaní k tomúto achievementu, aby ste mohli plniť podúlohy!" data-original-title="Nemožno splniť">Splniť</a>
                                        <script>$(function () { $("#complete{{sub.id}}Btn").popover();});</script>
                                    {% endif %}
                                    </td>
                                </tr>
                            {% endfor %}
                            <tbody>
                        </table>
                        
                        </div>
                        
                    </div>
                </div>
                
            {% endif %}
            
            
            {% get_comment_list for achievement as comment_list %}
                
            <div class="well well-small">
            <legend><i class="icon-white icon-comment" style="margin-top:5px;"></i> Komentáre</legend>
            {% if comment_list %}
                <table class="table table-condensed">
                    {% for comment in comment_list reversed %}
                        <tr><td>
                            <table class="table table-stripped">
                                <tr><td style="border:0px;">
                                    <a href="{% url show_user user_id=comment.user.username %}"><span class="label label-inverse">{{comment.user}}</span></a> dňa {{comment.submit_date.date}} o {{comment.submit_date.time}}
                                </td></tr>
                                <tr><td style="border:0px;">
                                    <p>{{comment.comment}}</p>
                                </td></tr>
                            </table>
                        </td></tr>
                    {% endfor %}
                </table>
            {% else %}
                <p>K tomuto achievementu neboli pridané žiadne komentáre.</p>
            {% endif %}
            
            <div class="container-fluid">
            <div class="row-fluid">
            
            {% if user.is_authenticated %}
                {% get_comment_form for achievement as form %}
                
                <br/>
                
                <form class="well well-small" action="{% comment_form_target %}" method="POST">{% csrf_token %}
                    <legend>Pridať komentár</legend>
                    
                    <input type="hidden" name="name" maxlength="50" value="{{ user.username }}" />
                    <input type="hidden" name="email" value="{% if user.email %}{{ user.email }}{% else %}bar@foo.com{% endif %}" />
                    <input type="hidden" name="url" value="" />
                    
                    <div class="container-fluid">
                        <div class="row-fluid">

                        
                            <textarea class="span12" rows="3" cols="80" name="comment" placeholder="Text komentáru..."></textarea>
                        
                            <div class="row-fluid">
                                <input class="btn btn-large" type="submit" name="submit" value="Odoslať">
                            </div>
                        
                        </div>
                    </div>
                    
                    <input type="text" name="honeypot" style="display:none;" />
                    
                    {{ form.content_type }}
                    {{ form.object_pk }}
                    {{ form.timestamp }}
                    {{ form.security_hash }}
                    
                    <input type="hidden" name="next" value="/achievement/{{achievement.uuid}}/" />
                    
                    
                    
                </form>
                
                
                
            {% else %}
                <div class="well well-small">
                <p>Prosím <a href="{% url accounts_login %}">prihláste sa</a> na zanechanie komentáru.</p>
                </div>
            {% endif %}
            
            </div>
            </div>
            
            </div>
            
        </div>
        
        
    {% else %}
        <div class="alert alert-error">
            <strong>Chyba!</strong> Tento achievement nebol nájdený.
        </div>
    {% endif %}

{% endblock %}

